package com.biz.primus.ms.depot.api;

import com.biz.primus.base.vo.PageResult;
import com.biz.primus.base.vo.PageVo;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.model.depot.enums.BusinessStatus;
import com.biz.primus.model.depot.vo.*;
import com.biz.primus.ms.base.api.BaseApiController;
import com.biz.primus.ms.depot.service.DepotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * DepotApiController
 *
 * @author jia JKLiues
 * @date 2017年09月20日
 */
@Api("门店基础服务")
@RestController
@RequestMapping("soa/depotService")
public class DepotApiController extends BaseApiController {

    private final DepotService depotService;

    @Autowired
    DepotApiController(DepotService depotService) {
        this.depotService = depotService;
    }


    @ApiOperation(value = "构建门店全量索引", notes = "异步构建")
    @PostMapping("buildFullIndex")
    public void buildFullIndex() {
        depotService.buildFullIndex();
    }

    @ApiOperation("构建门店全量索引数据")
    @PostMapping("createFullDepotEsVo")
    public PageResult<DepotEsIndexVo> createFullDepotEsVo(@RequestBody PageVo vo) {
        return depotService.createFullDepotEsVo(vo);
    }

    @ApiOperation("构建门店增量索引数据")
    @PostMapping("createIncrementDepotEsVo")
    public List<DepotEsIndexVo> createIncrementDepotEsVo(@RequestBody IncrementDepotEsIndexQueryVo vo) {
        return depotService.createIncrementDepotEsVo(vo);
    }

    @ApiOperation("门店搜索")
    @PostMapping("searchDepots")
    public SpringPageVO<DepotListRespVo> searchDepots(DepotQueryReqVo reqVo) {
        return depotService.searchDepots(reqVo);
    }

    @ApiOperation(value = "获取指定ID的门店信息Mysql", notes = "不返回门店起送价和运费策略")
    @GetMapping("findByIdFDb")
    public DepotDetailRespVo findByIdFDb(
            @ApiParam(value = "门店ID", required = true) @RequestParam("depotId") Long depotId) {
        return depotService.findByIdFDb(depotId);
    }

    @ApiOperation(value = "从redis中获取指定门店编码的门店", notes = "返回门店起送价和运费策略")
    @GetMapping("getByDepotCodeFRdb")
    public Optional<DepotRespVo> getByDepotCodeFRdb(
            @ApiParam(value = "门店编码", required = true) @RequestParam("depotCode") String depotCode) {
        return depotService.getByDepotCodeFRdb(depotCode);
    }

    @ApiOperation("从mysql中获取指定门店编码的门店")
    @GetMapping("getByDepotCodeFDb")
    public Optional<DepotRespVo> getByDepotCodeFDb(
            @ApiParam(value = "门店编码", required = true) @RequestParam("depotCode") String depotCode) {
        return depotService.getByDepotCodeFDb(depotCode);
    }

    @ApiOperation("获取门店的附近门店")
    @GetMapping("getDepotNearDepots")
    public List<DepotRespVo> getDepotNearDepots(@ApiParam(value = "门店编码") @RequestParam("depotCode") String depotCode) {
        return depotService.getDepotNearDepots(depotCode);
    }

    @ApiOperation("获取指定ID的门店是否营业")
    @GetMapping("getDepotBusinessStatus")
    public Boolean getDepotBusinessStatus(
            @ApiParam(value = "门店ID") @RequestParam("depotId") Long depotId) {
        return depotService.getDepotBusinessStatus(depotId);
    }

    @ApiOperation("修改门店营业状态")
    @GetMapping("updateDepotBusinessStatus")
    public void updateDepotBusinessStatus(
            @ApiParam(value = "门店ID") @RequestParam("depotId") Long depotId,
            @ApiParam(value = "门店ID") @RequestParam(value = "depotId", defaultValue = "ON") BusinessStatus status
    ) {
        depotService.updateDepotBusinessStatus(depotId, status);
    }

    @ApiOperation("同步Redis")
    @PostMapping("syn2Redis")
    public void syncMysql2Redis() {
        depotService.syncMysql2Redis();
    }

    @ApiOperation("获取门店信息")
    @GetMapping("findDepotsByDepotCodesFRdb")
    public List<DepotRespVo> findDepotsByDepotCodesFRdb(
            @ApiParam(value = "门店编码集") @RequestParam("depotCodes") List<String> depotCodes) {
        return depotService.findDepotsByDepotCodesFRdb(depotCodes);
    }

    @ApiOperation("获取最近的门店")
    @PostMapping("searchNearbyDepot")
    public SearchDepotResVo searchNearbyDepot(@RequestBody SearchDepotConditionVo vo) {
        return depotService.searchNearbyDepot(vo);
    }

    @ApiOperation("获取所有的总仓")
    @GetMapping("getAllCenterDepots")
    public List<DepotRespVo> getAllCenterDepots() {
        return depotService.getAllCenterDepots();
    }

    @ApiOperation("获取所有的本仓")
    @GetMapping("getAllHomeDepots")
    public List<DepotRespVo> getAllHomeDepots() {
        return depotService.getAllHomeDepots();
    }

    @ApiOperation("搜索附近门店")
    @PostMapping("findNearDepot")
    public List<DepotRespVo> findNearDepot(@RequestBody SearchDepotConditionVo vo) {
        return depotService.findNearDepot(vo);
    }

    @ApiModelProperty("查redis中所有的门店")
    @GetMapping("findAllDepotsFRdb")
    public List<DepotRespVo> findAllDepotsFRdb() {
        return depotService.findAllDepotsFRdb();
    }


    @ApiOperation(value = "门店同步处理方法", notes = "多个门店")
    @PostMapping("handleErpDepots")
    public void handleErpDepots(List<IncrDepotReqVo> reqVos) {
        depotService.handleErpDepots(reqVos);
    }

    @ApiOperation("获取指定区域的门店")
    @GetMapping("findDepotsByGeoId")
    public List<DepotRespVo> findDepotsByGeoId(@ApiParam(value = "区域ID") @RequestParam("geoId") Long geoId) {
        return depotService.findDepotsByGeoId(geoId);
    }

    @ApiOperation("获取运费策略")
    @GetMapping("getAllAvailableFreight")
    public List<FreightTemplateDetailRespVo> getAllAvailableFreight() {
        return depotService.getAllAvailableFreight();
    }


}